# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html


# useful for handling different item types with a single interface
from itemadapter import ItemAdapter
from openpyxl import Workbook


class CrawlnewsPipeline:
    def process_item(self, item, spider):
        # try:
        #     line = str(dict(item)) + '\n'
        #     print(line)
        #     self.f.write(line)
        # except:
        #     pass

        self.cnt += 1
        self.ws.append([str(self.cnt), item['newsTitle'], item['newsDate'], item['newsSource'], item['newsCategory'],
                        item['newsGroup'], item['newsLocation'], item['newsLink'], item['newsContent']])
        return item

    def open_spider(self, spider):
        # self.f = open('NewsInfo.txt', 'w')

        self.wb = Workbook()
        self.ws = self.wb.create_sheet('most.gov', 0)
        self.ws.append(['序号', '标题', '日期', '来源', '分类', '网站分组', '地方', '链接', '内容'])
        self.cnt = 0

    def close_spider(self, spider):
        # self.f.close()

        self.wb.save('news_list.xlsx')
